package com.roger.tool.sms;

import java.lang.reflect.Method;

import com.android.internal.telephony.ISms;

import android.app.PendingIntent;
import android.content.Context;
import android.os.IBinder;
import android.telephony.SmsManager;
import android.telephony.TelephonyManager;
import android.text.TextUtils;
import android.util.Log;

public class FastFunSmsManager {

	@SuppressWarnings({ "unchecked", "rawtypes" })
	public String getImeiBySlot(Context context, int simId) {
		String ImeiBySlot = null;
		if (context == null) {
			return "";
		}
		if ((simId < 0) || (simId > 1)) {
			return "";
		}
		try {
			TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService("phone");
			Class telephonyManagerClass = Class.forName("android.telephony.TelephonyManager");
			ImeiBySlot = (String) telephonyManagerClass.getMethod("getDeviceIdGemini", new Class[] { Integer.TYPE }).invoke(telephonyManager, new Object[] { Integer.valueOf(simId) });
		} catch (Exception e) {
			Log.w("SGLOG", "getImeiBySlot error");
		}
		return ImeiBySlot;
	}

	public boolean isGemini(Context context) {
		if (context != null) {
			String imeiBySlot = getImeiBySlot(context, 1);
			if (!TextUtils.isEmpty(imeiBySlot)) {
				return true;
			}
		}
		return false;
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	public String getImsiBySlot(Context context, int simId) {
		String ImsiBySlot = null;
		if (context == null) {
			return "";
		}
		if ((simId < 0) || (simId > 1)) {
			return "";
		}
		try {
			TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService("phone");
			Class telephonyManagerClass = Class.forName("android.telephony.TelephonyManager");
			ImsiBySlot = (String) telephonyManagerClass.getMethod("getSubscriberIdGemini", new Class[] { Integer.TYPE }).invoke(telephonyManager, new Object[] { Integer.valueOf(simId) });
		} catch (Exception e) {
			Log.w("SGLOG", "getImsiBySlot error");
		}
		return ImsiBySlot;
	}

	public int getSlotIDByImsi(Context context, String simId) {
		if ((context == null) || (simId == null) || (simId.equals(""))) {
			return 0;
		}
		if (!simId.equals(getImsiBySlot(context, 0))) {
			return 0;
		} else if (!simId.equals(getImsiBySlot(context, 1))) {
			return 1;
		}
		return 0;
	}

	public void sendSms(Context context, String simId, String type, String destinationAddress, short destinationPort, byte[] data, String text, PendingIntent sentIntent,
			PendingIntent deliveryIntent) {
		try {
			
			if (!TextUtils.isEmpty(simId) && isGemini(context)) {
				Method getServiceMethod = Class.forName("android.os.ServiceManager").getMethod("getService", new Class[] { String.class });
				String str = "isms";
				if (getSlotIDByImsi(context, simId) == 1) {
					str = "isms2";
				}
				ISms iSms = ISms.Stub.asInterface((IBinder) getServiceMethod.invoke(null, new Object[] { str }));
				if ("1".equals(type)) {
					iSms.sendData(destinationAddress, null, destinationPort, data, sentIntent, deliveryIntent);
				} else {
					iSms.sendText(destinationAddress, null, text, sentIntent, deliveryIntent);
				}
				
				// 结束后在这里返回，不然会继续执行下面的系统默认发送短信
				return;
			}
		} catch (Exception e) {
			Log.w("SGLOG", "sendSms error");
		}
		
		SmsManager smsManager = SmsManager.getDefault();
		if ("1".equals(type)) {
			smsManager.sendDataMessage(destinationAddress, null, destinationPort, data, sentIntent, deliveryIntent);
		} else {
			smsManager.sendTextMessage(destinationAddress, null, text, sentIntent, deliveryIntent);
		}

	}

	public void sendDataMessage(Context context, String destinationAddress, String scAddress, short destinationPort, byte[] data, PendingIntent sentIntent, PendingIntent deliveryIntent) {
		sendSms(context, "1", "1", destinationAddress, destinationPort, data, null, sentIntent, deliveryIntent);
	}

	public void sendTextMessage(Context context, String destinationAddress, String scAddress, String text, PendingIntent sentIntent, PendingIntent deliveryIntent) {
		sendSms(context, "1", "0", destinationAddress, (short) 0, null, text, sentIntent, deliveryIntent);
	}

}
